<?php
class AreasController extends AreasAppController {
	public $uses = array ('Areas.Areas' );
	
	public function beforeFilter(){
		$this->Auth->allow('getSonOption');
		parent::beforeFilter();
	}
	
	/**
	 * 地区列表
	 * @author lj@gootop.net
	 * @Date 2012-11-29
	 * @param
	 * @return
	 */
	public function admin_index() {
		$url = array('admin'=>true,'plugin'=>'areas','controller'=>'areas','action'=>'ajaxJsonPage');
		$param = $this->Areas->pageParam($url);
		$this->set('paginationScript',$this->Areas->showTreePagination($param));
	}
	
	/**
	 *
	 * 用户查询数据
	 * @author lj@gootop.net
	 * @Date 2012-11-29
	 * @param
	 * @return
	 */
	function admin_ajaxJsonPage(){
		$this->autoRender = false;
	
		$return = array(
			'page'=>0,			 
			'total'=>0,			 
			'records'=>0,		 
			'rows'=>array()		 
		);
		$parentId = empty($_POST['nodeid'])?0:$_POST['nodeid'];
		$level = empty($_POST['n_level'])?0:$_POST['n_level'];
		$rows = $this->Areas->ajaxTreeCategory($parentId,$level+1);
		$return['rows'] =$rows;
		
		echo json_encode($return);
	}
	/**
	 * 添加地区
	 * @author lj@gootop.net
	 * @Date 2012-11-29
	 * @param
	 * @return
	 */
	function admin_add($parent_id=0) {
		if ($this->request->is('post')) {
			$this->Areas->create();
			if ($this->Areas->save($this->data)) {
				$this->Session->setFlash(__('操作成功！'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('操作失败！'));
			}
		}
		$this->set('parent_id',$parent_id);
	}
	/**
	 * 修改地区
	 * @author lj@gootop.net
	 * @Date 2012-11-29
	 * @param
	 * @return
	 */
	function admin_edit($id = null) {
		$this->Areas->id = $id;
		if (!$this->Areas->exists()) {
			throw new NotFoundException(__('Invalid sys area'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Areas->save($this->request->data)) {
				$this->Session->setFlash(__('操作成功！'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('操作失败！'));
			}
		} else {
			$this->request->data = $this->Areas->read(null, $id);
		}
	}
	
	/**
	 * 通过上级id取得下级地区
	 * @author zmx@gootop.net  
	 * @Date 2012-12-4
	 * @param
	 * @return 
	 */
	function getAreaByPid($parent_id = 139){
		return $this->Areas->findAreaList($parent_id);
	}
	
	/**
	 * 取得拉菜单
	 * @author mingxing.zhao@qq.com
	 * @date 2013-12-23 下午9:36:40
	 * @param variable
	 * @return 
	 */
	function admin_getSonOption($parent_id = 139,$select_id = false,$empty = false){
		$this->set('select_id',$select_id);
		$this->set('empty',$empty);
		$this->set('data',$this->Areas->findAreaList($parent_id));
		$this->set('state',true);
	}
	
	/**
	 * 取得拉菜单
	 * @author mingxing.zhao@qq.com
	 * @date 2013-12-23 下午9:36:40
	 * @param variable
	 * @return
	 */
	function getSonOption($parent_id = 139,$select_id = false,$empty = false){
		$this->set('select_id',$select_id);
		$this->set('empty',$empty);
		$this->set('data',$this->Areas->findAreaList($parent_id));
		$this->set('state',true);
	}
}
